package com.ruoyi.oto.task;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.oto.domain.vo.NewsVO;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component("SsTask")
@Slf4j
public class OTOTask {

    @Resource
    RedisCache redisCache;


    /**
     * 获取九华新闻要点
     */
    @Scheduled(cron = "0 0 12 * * *")
    public void geJXNews() throws IOException {
        List<NewsVO> list = new ArrayList<>();
        Document document = Jsoup.connect("https://jiuhua.rednet.cn/m/").get();
        Element element = document.getElementById("div_jhyw");
        Elements elements = element.getElementsByClass("img_text");
        for(Element ele: elements){
            String text = ele.getElementsByClass("list_title").get(0).text();
            Element elementA = ele.getElementsByClass("list_title").get(0).getElementsByTag("a").get(0);
            String href = elementA.attr("href");
            String imgUrl = ele.getElementsByTag("img").get(0).attr("src");
            list.add(new NewsVO(text,imgUrl,href));
        }
        redisCache.deleteObject("newsList");
        redisCache.setCacheList("newsList",list);
    }
}
